from django.shortcuts import render
from django.core.paginator import Paginator
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import redirect
from django.core.urlresolvers import reverse

from common.models import Product,Category,Shop,User,Orders,OrderDetail,Payment,Member

from datetime import datetime

def insert(request):
    try:
        # 执行订单信息添加操作
        od = Orders()
        od.shop_id = request.session['shopinfo']['id']  # 店铺id
        od.member_id = 0  # 会员id
        od.user_id = request.session['vipuser']['id']  # 操作员id
        od.money = request.session['totalprice']  # 金额
        od.status = 1  # 订单状态
        od.payment_status = 2  # 支付状态
        od.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")  # 添加时间
        od.save()

        # 执行支付信息添加
        pt = Payment()
        pt.order_id = od.id  # 订单id
        pt.member_id = 0  # 会员id
        pt.money = request.session['totalprice']  # 金额(8,2)
        pt.type = 2  # 付款方式
        pt.bank = request.GET.get("bank")  # 收款银行渠道
        pt.status = 2  # 支付状态
        pt.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")  # 添加时间
        pt.save()

        # 执行订单详情添加
        orderslist = request.session['foodlist']
        for food in orderslist.values():
            ov = OrderDetail() 
            ov.order_id = od.id # 订单id号
            ov.product_id = food['id'] # 菜品id号
            ov.product_name = food['name'] # 菜品名称
            ov.price = food['price'] # 单价
            ov.quantity = food['m'] # 数量
            ov.status = 1
            ov.save()
        
        del request.session['foodlist']
        del request.session['totalprice']
        res = 'Y'
        return HttpResponse(res)
    except Exception as err:
        print(err)
        return HttpResponse("执行订单添加发生错误！")

def odindex(request,pIndex=1):
    '''查看订单'''
    context = {}
    odstatus = request.GET.get("status")
    if int(odstatus) == 1:
        pagetype = 1
    else:
        pagetype = 2
    odlist = Orders.objects.filter(shop_id=request.session['shopinfo']['id'],status=odstatus)
    #遍历订单信息，查询每个订单下对应的操作员，并以uids,mids属性存放
    for vo in odlist:
        vo.user_name = User.objects.get(id=vo.user_id).nickname
        vo.ptype = Payment.objects.get(order_id=vo.id).type
        vo.bank = Payment.objects.get(order_id=vo.id).bank
    # 分页显示
    p = Paginator(odlist, 10)
    if pIndex == "":
        pIndex = 1
    odlist = p.page(pIndex)
    plist = p.page_range
    print(plist)
    context = {"odlist":odlist,"plist":plist,"pIndex":int(pIndex),"pagetype":int(pagetype),"status":odstatus}
    return render(request,"web/list.html",context)

def oddetail(request):
    '''查看订单详情'''
    try:
        oddetail = OrderDetail.objects.filter(order_id=request.GET.get("oid"))
        context={"oddetail":oddetail}
        return render(request,"web/detail.html",context)
    except Exception as err:
        print(err)
        return HttpResponse("订单不存在！")

def odstatus(request):
    '''修改订单状态'''
    try:
        od = Orders.objects.get(id=request.GET.get("oid"))
        od.status = request.GET.get("status")
        od.save()
        data="Y"
        return HttpResponse(data)
    except Exception as err:
        print(err)
        return HttpResponse("订单不存在！")